import { fetchGeneralConfigAPI } from '@/apis/fetchGeneralConfig'
import type { GeneralConfig } from '@/types/api'

export const useGeneralConfigStore = defineStore(
  'generalConfig',
  () => {
    const generalConfig = ref<GeneralConfig>()

    const setgeneralConfig = (newVal?: GeneralConfig) => {
      generalConfig.value = newVal
    }

    const update = async () => {
      const generalConfigRes = await fetchGeneralConfigAPI()

      if (generalConfigRes.data?.code === 1)
        setgeneralConfig(generalConfigRes.data.data)
      else
        uni.showToast({ title: generalConfigRes.data?.msg, icon: 'none' })
    }

    const $reset = () => setgeneralConfig()

    return { generalConfig, setgeneralConfig, update, $reset }
  },
  {
    persist: true,
  },
)
